//
//  HMVisitorView.swift
//  MicroBlog
//
//  Created by Apple on 16/9/20.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
//定义代理
protocol HMVisitorViewDelegate: NSObjectProtocol{
    func didSelectedLogin()
}
//访客视图
class HMVisitorView: UIView {
    
    //需要传入执行的闭包
    var loginClosoure: (()->())?
    //定义代理
    weak var delegate: HMVisitorViewDelegate?
    //MARK: -懒加载控件
    //旋转视图
    private lazy var cycleImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //罩层
    private lazy var maskImageV: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //主页
    private lazy var iconImageV:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //未登录信息视图
    private lazy var messageLabel: UILabel = {
       let label = UILabel()
        label.text = "关注一些人,回这里看看有什么惊喜关注一些人,回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.center
        return label
    }()
    
    //登录按钮
    private lazy var loginButton: UIButton = {
       let button = UIButton()
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    //注册按钮
    private lazy var registButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        //添加子控件
        addSubview(cycleImageV)
        addSubview(maskImageV)
        addSubview(iconImageV)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registButton)
        //使用系统自动布局
//        cycleImageV.translatesAutoresizingMaskIntoConstraints = false
        //设置布局
//        addConstraint(NSLayoutConstraint(item: cycleImageV, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
//        
//        addConstraint(NSLayoutConstraint(item: cycleImageV, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        //设置约束
        cycleImageV.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskImageV.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        iconImageV.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(cycleImageV)
            make.top.equalTo(cycleImageV.snp_bottom)
            make.width.equalTo(224)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        registButton.snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
        }
    }
    
    //修饰访客视图信息
    func updataVisitorViewInfo(message: String?, imageName: String?) {
        if let msg = message, let imgName = imageName {
            //如果代码执行到此,表示消息,发现,我的视图控制器
            messageLabel.text = msg
            iconImageV.image = UIImage(named: imgName)
            cycleImageV.isHidden = true
        }else{
            //表示首页vc,执行旋转动画
            startAnimation()
        }
        
    }
    
    //开启动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //执行到的目的地
        animation.toValue = 2 * M_PI
        //动画执行的时长
        animation.duration = 20
        //动画执行的次数
        animation.repeatCount = MAXFLOAT
        //失去焦点让其不释放
        animation.isRemovedOnCompletion = false
        cycleImageV.layer.add(animation, forKey: nil)
    }
    
    //点击事件
    @objc private func loginAction() {
        print("我是访客视图点击的操作")
        //执行闭包
        loginClosoure?()
        //执行代理
        delegate?.didSelectedLogin()
    }

}
